-- Default roles: Administrators, Users
insert into DcRole values( default, "Administrators");
set @adminRole = last_insert_id();
insert into DcRole values( default, "Users");
set @userRole = last_insert_id();

-- Default department: Common
insert into Department (Name) Values ('Common');
set @commonDepartment = last_insert_id();


-- Default users: DocCircAdmin, DocCircService, DocCircUser
insert into DcUser values (default, "DocCircAdmin", PASSWORD("1"), "DocCirc Administrator", false, true, @commonDepartment, false, "", false, false, "");
set @lastUser = last_insert_id();
insert into UserEmail values (default, @lastUser, "doccircadmin@debug.org");
insert into RoleToUser values (default, @adminRole, @lastUser, 1);

insert into DcUser values (default, "DocCircService", PASSWORD("1"), "DocCirc service user", false, true, @commonDepartment, false, "", false, false, "");
set @lastUser = last_insert_id();
insert into UserEmail values (default, @lastUser, "doccircservice@debug.org");
insert into RoleToUser values (default, @adminRole, @lastUser, 1);

insert into DcUser values (default, "DocCircUser", PASSWORD("1"), "DocCirc user", false, true, @commonDepartment, false, "", false, false, "");
set @lastUser = last_insert_id();
insert into UserEmail values (default, @lastUser, "doccircuser@debug.org");
insert into RoleToUser values (default, @userRole, @lastUser, 1);

-- Folder types: Document, DocType
insert into FolderType (FolderTypeId, Name) Values (default, 'Document');
set @documentFolderType = last_insert_id();
insert into FolderType (FolderTypeId, Name) Values (default, 'DocType');
set @docTypeFolderType = last_insert_id();

-- RouteItemTypes: Decision, Notification
insert into RouteItemType (Name) Values ('Decision');
insert into RouteItemType (Name) Values ('Notification');

-- DocStatus: Document statuses
insert into DocStatus (Name) Values ('NotSavedDocStatus');
insert into DocStatus (Name) Values ('DraftDocStatus');
insert into DocStatus (Name) Values ('UnderConsDocStatus');
insert into DocStatus (Name) Values ('FinalizedDocStatus');

-- NotificationType: Notification types
insert into NotificationType (Name) Values ('NextDecision');
insert into NotificationType (Name) Values ('DeputyNextDecision');
insert into NotificationType (Name) Values ('NextNotification');
insert into NotificationType (Name) Values ('DecisionAfterReturn');
insert into NotificationType (Name) Values ('NotificationAfterReturn');
insert into NotificationType (Name) Values ('Finalization');
insert into NotificationType (Name) Values ('ReturnToDraftDueToDecision');
insert into NotificationType (Name) Values ('ReturnToDraftDueToRouteChange');
insert into NotificationType (Name) Values ('AcquaintanceRequest');

-- EventType: Event types
insert into EventType (Name) Values ('PassDecision');
insert into EventType (Name) Values ('PassDecisionAutomatically');
insert into EventType (Name) Values ('PassDecisionByDeputy');
insert into EventType (Name) Values ('RejectDecision');
insert into EventType (Name) Values ('RejectDecisionByDeputy');
insert into EventType (Name) Values ('RejectDecisionByAdministrator');
insert into EventType (Name) Values ('PostponeDecision');
insert into EventType (Name) Values ('PostponeDecisionByDeputy');
insert into EventType (Name) Values ('PostponeDecisionByAdministrator');
insert into EventType (Name) Values ('ReturnToDraftDueToRouteChange');
insert into EventType (Name) Values ('DocCardRegistration');
insert into EventType (Name) Values ('DocCardFinalization');
insert into EventType (Name) Values ('ConsiderationStarted');
insert into EventType (Name) Values ('PassedToNextRoutePoint');

-- permissions for new document type and document folder
insert into NewFolderPermission (NewFolderPermissionId, FolderType, DcRole, C, R, U, D, B, RO, UO, DO) Values
--										       C	  R     U	   D      B     RO    UO     DO	   
	(default, @docTypeFolderType, @adminRole, true,  true, true,  true,  true, true, true,  true),
	(default, @docTypeFolderType, @userRole,  false, true, false, false, true, true, false, false),
	(default, @documentFolderType, @adminRole,true,  true, true,  true,  true, true, true,  true),
	(default, @documentFolderType, @userRole, true,  true, true,  true,  true, true, true,  true);
	
-- permissions for new document type
insert into NewDocTypePermission (NewDocTypePermissionId, DcRole, C, R, U, D, RO, UO, DO) Values
--						   C	  R    U	  D    RO    UO    DO	 
	(default, @adminRole, true, true, true, true, true, true, true),
	(default, @userRole,  true, true, true, true, true, true, true);

-- Root folder for documents
insert into Folder (FolderId, ParentFolder, Name, FolderType) Values 
	(default, null, "^DocumentFolderName^", @documentFolderType);
set @documentRootFolder = last_insert_id();

insert into FolderPermission (Folder, DcRole, C, R, U, D, B, RO, UO, DO)
	select @documentRootFolder, DcRole, C, R, U, D, B, RO, UO, DO from NewFolderPermission where FolderType=@documentFolderType;

-- Root folder for document types
insert into Folder (FolderId, ParentFolder, Name, FolderType) 
	Values (default, null, "^DocTypeFolderName^", @docTypeFolderType);
set @docTypeRootFolder = last_insert_id();

insert into FolderPermission (Folder, DcRole, C, R, U, D, B, RO, UO, DO)
	select @docTypeRootFolder, DcRole, C, R, U, D, B, RO, UO, DO from NewFolderPermission where FolderType=@docTypeFolderType;

